package com.ph.permission.api.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ph.base.BaseController;
import com.ph.shopping.common.core.customenum.ResultEnum;
import com.ph.shopping.common.core.customenum.SmsCodeType;
import com.ph.shopping.common.core.other.bankcard.BankCardCheckService;
import com.ph.shopping.common.util.result.Result;
import com.ph.shopping.facade.member.entity.BankCodeAndName;
import com.ph.shopping.facade.member.service.IBankCardBindService;
import com.ph.shopping.facade.member.service.ISmsCodeSendService;
import com.ph.shopping.facade.permission.entity.ManageBankCardInfo;
import com.ph.shopping.facade.permission.service.IManageBankCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * @项目：phshopping-api-platform
 * @描述：绑定银行卡controller
 * @作者： ShuHao
 * @创建时间：2017年3月28日
 * @Copyright @2017 by ShuHao
 */
@Controller
@RequestMapping("api/bankCard")
public class BankCardController extends BaseController {

    @Reference(version = "1.0.0")
    private IManageBankCardService iBankCardService;

    @Reference(version = "1.0.0")
    private IBankCardBindService iBankCardBindService;

    @Reference(version = "1.0.0", timeout = 30000)
    private ISmsCodeSendService iSmsCodeSendService;

    @Autowired
    private BankCardCheckService bankCardCheckService;

    /**
     * 跳转绑定银行卡页面
     *
     * @return
     */
    @RequestMapping(value = "/bankCardPage", method = RequestMethod.GET)
    public Object bankCardPage() {
        try {
            ModelAndView mv = new ModelAndView("/account/bankCard/bankCard");
            ManageBankCardInfo bankCardInfo = new ManageBankCardInfo();
            bankCardInfo.setManagerId(getLoginUser().getId());
            bankCardInfo.setBindPhone(getLoginUser().getTelphone());
            Result result = iBankCardService.getBindCardInfo(bankCardInfo);
            mv.addObject("bindCardInfo", result.getData());
            return mv;
        } catch (Exception e) {
            logger.error("获取银行卡信息异常:", e.getMessage());
            return "";
        }
    }


    /**
     * @param bankCardInfo
     * @return
     * @获取用户绑定的银行卡信息
     */
    @RequestMapping(value = "/getBindCardInfo", method = RequestMethod.GET)
    @ResponseBody
    public Object getBindCardInfo(@ModelAttribute ManageBankCardInfo bankCardInfo) {
        bankCardInfo.setManagerId(getLoginUser().getId());
        bankCardInfo.setBindPhone(getLoginUser().getTelphone());
        return iBankCardService.getBindCardInfo(bankCardInfo);

    }

    /**
     * @param bankCardInfo
     * @return
     * @绑定银行卡
     */
    @RequestMapping(value = "/bindCard", method = RequestMethod.POST)
    @ResponseBody
    public Object bindCard(@ModelAttribute ManageBankCardInfo bankCardInfo, String verificationCode) {
        try {
            //三要素检验
            String idCard = bankCardInfo.getIdCard();
            String cardNum = bankCardInfo.getCardNum();
            String ownName = bankCardInfo.getOwnName();
            if (idCard != null && cardNum != null && ownName != null) {
                Boolean result = bankCardCheckService.bankCardAuth(idCard, cardNum, ownName);
                if (!result) {
                    return getResult(ResultEnum.BANK_CHECK_FAIL);
                }
            }

            //校验验证码
            Result result = iSmsCodeSendService.verifySmsCode(getLoginUser().getTelphone(), SmsCodeType.BIND_MEMBERBANK.getCodeType(), verificationCode);
            if ("200".equals(result.getCode())) {
                return iBankCardService.bindCard(bankCardInfo, getLoginUser());
            } else {
                return getResult(ResultEnum.CHECKBIND_EXCEPTION);
            }
        } catch (Exception e) {
            logger.error("绑卡异常:", e.getMessage());
            return getResult(ResultEnum.BINDCARD_EXCEPTION);
        }



    }

    /**
     * @param bankCardInfo
     * @return
     * @解绑银行卡
     */
    @RequestMapping(value = "/unBindCard", method = RequestMethod.POST)
    @ResponseBody
    public Object unBindCard(@ModelAttribute ManageBankCardInfo bankCardInfo, String verificationCode) {

        try {
            //校验验证码
            Result result = iSmsCodeSendService.verifySmsCode(getLoginUser().getTelphone(), SmsCodeType.BIND_MEMBERBANK.getCodeType(), verificationCode);
            if ("200".equals(result.getCode())) {
                return iBankCardService.unBindCard(bankCardInfo, getLoginUser());
            } else {
                return getResult(ResultEnum.INTERNAL_SERVER_ERROR);
            }
        } catch (Exception e) {
            logger.error("解绑异常:", e.getMessage());
            return getResult(ResultEnum.UNBINDCARD_EXCEPTION);
        }


    }

    /**
     * @param bankCardInfo
     * @return
     * @根据银行卡号自动获取银行名字
     */
    @RequestMapping(value = "/getBankName", method = RequestMethod.POST)
    @ResponseBody
    public Object getBankName(@ModelAttribute ManageBankCardInfo bankCardInfo) {

        try {
            String idCard = bankCardInfo.getIdCard();
            String cardNum = bankCardInfo.getCardNum();
            String ownName = bankCardInfo.getOwnName();
            if (idCard != null && cardNum != null && ownName != null) {
                Boolean isTrue = bankCardCheckService.bankCardAuth(idCard, cardNum, ownName);
                if (isTrue) {
                    Result result = iBankCardBindService.getBankCodeAndNameByCode(bankCardInfo.getCardNum());
                    if (result.getData() != null) {
                        String bankName = ((BankCodeAndName) result.getData()).getBankName();
                        return getResult(ResultEnum.SUCCESS, bankName);
                    }
                }
            }
            return getResult(ResultEnum.CHECK_BANK_EXCEPTION);
        } catch (Exception e) {
            logger.error("校验异常:", e.getMessage());
            return getResult(ResultEnum.INTERNAL_SERVER_ERROR);
        }

    }

    /**
     * @param bankCardInfo
     * @return
     * @发送验证码
     */
    @RequestMapping(value = "/sendValidCode", method = RequestMethod.POST)
    @ResponseBody
    public Object sendValidCode(@ModelAttribute ManageBankCardInfo bankCardInfo) {
        return iSmsCodeSendService.sendSmsCodeByNoMsg(getLoginUser().getTelphone(), SmsCodeType.BIND_MEMBERBANK.getCodeType());
    }


}
